package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {
	
	private final EmployeeMapper employeeMapper;
	
	public EmployeeServiceImpl(EmployeeMapper employeeMapper) {
		this.employeeMapper = employeeMapper;
	}
	
	/**
	 * 员工登录
	 *
	 * @param employeeLoginDTO
	 * @return
	 */
	public EmployeeLoginVO login(EmployeeLoginDTO employeeLoginDTO) {
		String username = employeeLoginDTO.getUsername();
		String password = employeeLoginDTO.getPassword();
		
		//1、根据用户名查询数据库中的数据
		Employee employee = employeeMapper.getByUsername(username);
		
		//2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
		if (employee == null) {
			//账号不存在
			throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
		}
		
		//密码比对
		// DONE 后期需要进行md5加密，然后再进行比对
		String passWordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
		if (!passWordMd5.equals(employee.getPassword())) {
			//密码错误
			throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
		}
		
		if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
			//账号被锁定
			throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
		}
		
		//将登录数据员工id存储到jwt令牌
		String jwt = JwtUtil.getToken(employee.getId());
		
		EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
				.id(employee.getId())
				.name(employee.getName())
				.userName(employee.getUsername())
				.token(jwt)
				.build();
		//3、返回实体对象
		return employeeLoginVO;
	}
	
	/**
	 * 分页查询
	 */
	@Override
	public PageResult page(EmployeePageQueryDTO employeePageQueryDTO) {
		if (employeePageQueryDTO.getPage() == null) {
			employeePageQueryDTO.setPage(1);
		}
		if (employeePageQueryDTO.getPageSize() == null) {
			employeePageQueryDTO.setPageSize(5);
		}
		PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
		List<Employee> employeePage = employeeMapper.getEmpBy(employeePageQueryDTO.getName());
		Page<Employee> p = (Page<Employee>) employeePage;
		PageResult pageResult = new PageResult();
		pageResult.setTotal(p.getTotal());
		pageResult.setRecords(p.getResult());
		return pageResult;
	}
	
	/**
	 * 根据id查询
	 */
	@Override
	public Result getEmpById(Long id) {
		if (id == null) {
			return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
		}
		Employee employee = employeeMapper.getEmpBy(null, id).remove(0);
		if (employee == null) {
			return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
		}
		return Result.success(employee);
	}
	
	/**
	 * 新增员工
	 */
	@Override
	public Result insert(EmployeeDTO employeeDTO) {
		if (employeeDTO.getUsername() == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		
		Employee employee = employeeMapper.getByUsername(employeeDTO.getUsername());
		if (employee != null) {
			return Result.error(MessageConstant.USER_ALREADY_EXISTS);
		} else {
			employee = new Employee();
		}
		employee.setStatus(StatusConstant.ENABLE);
		BeanUtils.copyProperties(employeeDTO, employee);
		return Result.success(employeeMapper.insert(employee));
	}
	
	/**
	 * 更新员工
	 */
	@Override
	public Result update(EmployeeDTO employeeDTO) {
		if (employeeDTO.getId() == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		Employee employee = new Employee();
		BeanUtils.copyProperties(employeeDTO, employee);
		employeeMapper.update(employee);
		return Result.success();
	}
	
	/**
	 * 修改员工账号活动状态
	 */
	@Override
	public Result banEmp(Integer status, Long id) {
		if (status == null || id == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		Employee employee = employeeMapper.getEmpBy(null, id).remove(0);
		employee.setStatus(status);
		employeeMapper.update(employee);
		return Result.success();
	}
	
}
